package com.wash.shoes.domain.mtbusy;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.meituan.sdk.MeituanResponse;
import com.meituan.sdk.internal.exceptions.MtSdkException;
import com.meituan.sdk.model.ddzhkh.dingdan.orderQueryInfo.AmountShare;
import com.meituan.sdk.model.ddzhkh.dingdan.orderQueryInfo.OrderQueryInfoRequest;
import com.meituan.sdk.model.ddzhkh.dingdan.orderQueryInfo.OrderQueryInfoResponse;
import com.meituan.sdk.model.ddzhkh.dingdan.orderQueryInfo.Receipt;
import com.wash.shoes.domain.vo.MtPushMessage;
import com.wash.shoes.entity.CheckHistoryPo;
import com.wash.shoes.entity.OrderPo;
import com.wash.shoes.entity.ShopPo;
import com.wash.shoes.service.CheckHistoryService;
import com.wash.shoes.service.ShopService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author lijianguo
 * @Date 16/09/2024
 * 预订核销
 **/
@Log4j2
@Component
public class Mt5810031Busy extends AbstractMtCallback {

    @Autowired
    CheckHistoryService checkHistoryService;

    @Autowired
    ShopService shopService;

    @Override
    public MtCallbackEnum getType() {
        return MtCallbackEnum.MT_5810031;
    }

    @Override
    public Integer processBusy(MtPushMessage message, String token, String developerId) {
        JSONObject jsonObject = JSONObject.parseObject(message.getMessage());
        String orderId = jsonObject.getString("orderId");
        String verifyStatus = jsonObject.getString("verifyStatus");
        String verifyChannel = jsonObject.getString("verifyChannel");

        OrderPo orderPo = orderService.getByMtOrderId(orderId);
        OrderQueryInfoRequest request = new OrderQueryInfoRequest();
        request.setOrderId(orderId);
        request.setUnifiedOrderId(orderPo.getMtUniOrderId());
        request.setType(Collections.singletonList(2));

        MeituanResponse<OrderQueryInfoResponse> response;
        try {
            response = MtAllClient.getClient(developerId).invokeApi(request, token);
            log.info("美团核销原始数据为: {}", JSON.toJSONString(response));
            OrderQueryInfoResponse infoResponse = response.getData();
            List<Receipt> receiptList = infoResponse.getReceiptList();
            Receipt receipt = receiptList.get(0);
            log.info("美团推送收据数据为: {}", JSON.toJSONString(receipt));
            long total = 0L;
            for (AmountShare amountShare : receipt.getAmountShareList()) {
                total += amountShare.getValue() / 100;
            }
            // 更新结算的信息
            orderPo.setSettlementPrice((double) total);
            orderPo.setSettlementStatus(verifyStatus);
            orderPo.setSettlementTime(new Date());
            orderService.updateById(orderPo);
            // 设置核销的记录
            try {
                log.info("记录核销的日志开始...");
                recordHis(orderPo);
                log.info("记录核销的日志结束...");
            } catch (Exception e) {
                log.error("记录日志错误....");
                log.error(e);
            }
            log.info("美团核销 request 参数为 {} ", JSONObject.toJSONString(request));
            log.info("美团核销 response 参数为 {} ", JSONObject.toJSONString(response));
        } catch (MtSdkException e) {
            log.error(e);
            throw new RuntimeException(e);
        }
        return 1;
    }


    /**
     * @author lijianguo
     * @Date 06/10/2024
     * 记录核销日志
     **/
    private void recordHis(OrderPo orderPo) {

        CheckHistoryPo checkHistoryPo = checkHistoryService.getByMtSerialNumbers(orderPo.getMtSerialNumbers());
        if (checkHistoryPo == null) {
            checkHistoryPo = new CheckHistoryPo();
        }


        checkHistoryPo.setShopId(orderPo.getShopId());
        ShopPo shopPo = shopService.getById(orderPo.getShopId());
        checkHistoryPo.setShopName(shopPo.getShopName());
        checkHistoryPo.setOrderId(orderPo.getId());
        checkHistoryPo.setOrderNo(orderPo.getOrderNo());
        checkHistoryPo.setMtOrderId(orderPo.getMtOrderId());
        checkHistoryPo.setMtSerialNumbers(orderPo.getMtSerialNumbers());
        checkHistoryPo.setPhone(orderPo.getCustomPhone());
        checkHistoryPo.setConpouPrice(orderPo.getSettlementPrice());

        checkHistoryService.saveOrUpdate(checkHistoryPo);

    }
}
